package com.zbkj.crmeb.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exception.CrmebException;
import com.google.common.collect.Lists;
import com.utils.DateUtil;
import com.zbkj.crmeb.user.dao.UserDao;
import com.zbkj.crmeb.user.dao.UserRelRecordDao;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.model.UserMark;
import com.zbkj.crmeb.user.model.UserRelRecord;
import com.zbkj.crmeb.user.request.UserRequest;
import com.zbkj.crmeb.user.service.UserMarkService;
import com.zbkj.crmeb.user.service.UserRelRecordService;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class UserRelRecordServiceImpl extends ServiceImpl<UserRelRecordDao, UserRelRecord> implements UserRelRecordService {

    @Resource
    private UserRelRecordDao dao;
    @Resource
    private UserDao userDao;
    @Resource
    private UserMarkService userMarkService;

    @Override
    public UserRelRecord getByUserId(Integer userId) {
        LambdaQueryWrapper<UserRelRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRelRecord::getUserId, userId);
        return dao.selectOne(wrapper);
    }

    @Override
    public List<UserRelRecord> findByUserIds(List<Integer> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<UserRelRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserRelRecord::getUserId, userIds);
        return dao.selectList(wrapper);
    }

    @Override
    public List<UserRelRecord> findByParentId(Integer parentId) {
        if (parentId == null) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<UserRelRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserRelRecord::getParentId, parentId);
        return dao.selectList(wrapper);
    }

    @Transactional
    @Override
    public void setParent(UserRequest userRequest) {
        if (userRequest.getUid().equals(userRequest.getParentId())) {
            throw new CrmebException("推荐人不能设置为本人");
        }
        User user = userDao.selectById(userRequest.getUid());
        if (user == null) {
            throw new CrmebException("用户不存在");
        }
        User parentUser = userDao.selectById(userRequest.getParentId());
        if (parentUser == null) {
            throw new CrmebException("推荐人不存在");
        }
        UserMark parentUserMark = userMarkService.getByUserId(userRequest.getParentId());
        if (parentUserMark == null) {
            throw new CrmebException("推荐人必须为会员");
        }
        // 更新用户推荐人
        user.setSpreadUid(userRequest.getParentId());
        user.setSpreadTime(DateUtil.nowDateTime());
        userDao.updateById(user);

        // 更新用户推荐人关系表（无责新增，有责更新）
        UserRelRecord userRelRecord = this.getByUserId(userRequest.getUid());
        if (userRelRecord == null) {
            userRelRecord = new UserRelRecord();
            userRelRecord.setUserId(userRequest.getUid());
            userRelRecord.setParentId(userRequest.getParentId());
            // 默认不生效（需要用户下单才能实时生效）
            userRelRecord.setEffectFlag(0);
            userRelRecord.setCreateTime(DateUtil.nowDateTime());
            dao.insert(userRelRecord);
        } else {
            userRelRecord.setUserId(userRequest.getUid());
            userRelRecord.setParentId(userRequest.getParentId());
            dao.updateById(userRelRecord);
        }
    }
}
